﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using VSA.Common;

namespace VSA.Applications
{
    public partial class PersonListForm : Form
    {
        private SearchGuideForm searchForm;
        public DataSet ResultSet { get; set; }

        public PersonListForm(SearchGuideForm searchForm, DataSet resultSet)
        {
            InitializeComponent();
            this.searchForm = searchForm;
            this.ResultSet = resultSet;
            
            this.gv_ContactPerson.DataSource = this.ResultSet.Tables[0];
        }

        private void gv_ContactPerson_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                PersonInfoForm personInfoForm = new PersonInfoForm(Convert.ToInt32(this.gv_ContactPerson.SelectedCells[0].Value));
                personInfoForm.CallType = 0;
                personInfoForm.Show();
            }
        }

        private void PersonListForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            searchForm.Show();
            this.Dispose();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.DefaultExt = "xls";
            saveFileDialog.Filter = "Excel file|*.xls";
            saveFileDialog.FileName = "Sheet1";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    new ExcelFile(this.ResultSet.Tables[0]).Create(saveFileDialog.FileName);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Error occur when open file, it might be opened！\n" + ex.Message);
                }

            }
        }
    }
}